package com.jxmcloud.business.cut.service.impl.app.gift;

import com.jxmcloud.business.ce.service.ICeSysParameterService;
import com.jxmcloud.business.cut.constants.CutConstants;
import com.jxmcloud.business.cut.constants.CutErrorMsg;
import com.jxmcloud.business.cut.dto.client.BuySpeedGiftDto;
import com.jxmcloud.business.cut.dto.client.GetGiftInfoDto;
import com.jxmcloud.business.cut.dtoCommon.CouponDto;
import com.jxmcloud.business.cut.mapper.database.IBackendGiftDBMapper;
import com.jxmcloud.business.cut.mapper.database.IRewardPoolDBMapper;
import com.jxmcloud.business.cut.mapper.database.IUserGiveUpAwardRecordDBMapper;
import com.jxmcloud.business.cut.service.app.gift.IGiftService;
import com.jxmcloud.business.cut.service.database.IUserActivityInfoDBService;
import com.jxmcloud.business.cut.service.database.IUserInviteSuccessDBService;
import com.jxmcloud.business.cut.service.impl.database.GiftBuyRecordDBServiceImpl;
import com.jxmcloud.business.cut.service.impl.logic.CouponHandleImpl;
import com.jxmcloud.business.cut.service.logic.IPayHandle;
import com.jxmcloud.business.cut.service.user.IUserGiftService;
import com.jxmcloud.business.cut.vo.client.BuySpeedGiftVo;
import com.jxmcloud.business.cut.vo.client.GetGiftInfoVo;
import com.jxmcloud.business.cut.vo.user.PlayerInfoVo;
import com.xlcloud.platform.api.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
@Slf4j
public class GiftServiceImpl implements IGiftService {
    @Autowired
    private IRewardPoolDBMapper rewardPoolDBMapper;

    @Autowired
    private IBackendGiftDBMapper backendGiftDBMapper;
    @Autowired
    private IUserGiveUpAwardRecordDBMapper userGiveUpAwardRecordDBMapper;
    @Autowired
    private IPayHandle payHandle;

    @Autowired
    private CouponHandleImpl couponHandle;

    @Autowired
    private GiftBuyRecordDBServiceImpl giftBuyRecordDBService;

    @Autowired
    private IUserGiftService userGiftService;

    @Autowired
    private ICeSysParameterService ceSysParameterService;

    @Resource
    private IUserInviteSuccessDBService inviteSuccessDBService;

    @Autowired
    private IUserActivityInfoDBService userActivityInfoDBService;
    //
    @Transactional(rollbackFor = Exception.class)
    @Override
    public BuySpeedGiftVo buySpeedGift(BuySpeedGiftDto dto, PlayerInfoVo player) {
        String error = CutErrorMsg.ERROR_FALSE;
        BuySpeedGiftVo result = new BuySpeedGiftVo();
        do {
            // 1. 判断用户支付密码是否正确
            boolean flag = payHandle.checkUserPassword(player, dto.getPassword());
            if (!flag) {
                error = "支付密码错误";
                break;
            }
            // 2. 判断用户是否有足够的余额
            flag = payHandle.checkUserBalance(player, dto.getPayPrice().longValue());
            if (!flag) {
                error = "余额不足";
                break;
            }
            // 3. check购买金额是否成功
            String money = ceSysParameterService.getParamByKeyCache(CutConstants.SPEED_GIFT_MONEY).getParameterValue();
            if (!money.equals(dto.getPayPrice().toString())) {
                error = "购买金额错误";
                break;
            }
            // 4. 购买加速礼包
            boolean b = userGiftService.buySpeedGift(dto, player);
            if (!b) {
                error = "购买失败";
            } else {
                error = CutErrorMsg.ERROR_TRUE;
            }

        } while (false);
        if (!error.equals(CutErrorMsg.ERROR_TRUE)) {
            // 抛出异常
            throw new BusinessException(error);
        }
        return result;
    }

    @Override
    public GetGiftInfoVo getGiftInfo(GetGiftInfoDto dto, PlayerInfoVo player) {
        String error = CutErrorMsg.ERROR_FALSE;
        GetGiftInfoVo result = new GetGiftInfoVo();
        do {
            // 获取加速礼包内容信息信息
            List<CouponDto> giftCoupon = userGiftService.getGiftCoupon(player);
            // 获取加速礼包金额
            String money = ceSysParameterService.getParamByKeyCache(CutConstants.SPEED_GIFT_MONEY).getParameterValue();
            result.setGiftMoney(Integer.parseInt(money));
            result.setList(giftCoupon);

            error = CutErrorMsg.ERROR_TRUE;
        } while (false);
        if (!error.equals(CutErrorMsg.ERROR_TRUE)) {
            // 抛出异常
            throw new BusinessException(error);
        }
        return result;
    }


}
